<?php

Yii::import('application.models._base.BaseTopic');

class Topic extends BaseTopic
{
    public $allContent;
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function rules() {
        return array(
            array('allContent', 'required','message'=>'-1'),
            array('allContent', 'length', 'max'=>280,'tooLong'=>'-2'),
            array('allContent', 'length', 'min'=>2,'tooShort'=>'-3'),
            array('content, ip, uid', 'required'),
//            array('ip, create, uid', 'numerical', 'integerOnly'=>true),
//            array('content', 'length', 'max'=>255),
//            array('content_over', 'length', 'max'=>25),
//            array('content_over', 'default', 'setOnEmpty' => true, 'value' => null),
            array('from_type, from_iid, reid, recount,content_over, create', 'safe'),
            array('from_type, from_iid, reid, recount,id, content, content_over, ip, create, uid', 'safe', 'on'=>'search'),
        );
    }
    public function relations() {
        return array(
            'author'=>array(self::BELONGS_TO,'User','uid'),
            'images'=>array(self::HAS_MANY,'WbImage','tid')
        );
    }




    //自动完成
    public function beforeSave(){
        //如果是新增
        if($this->isNewRecord){
            $this->create = time();
        }
        return true;
    }

    public function getList($first,$total){
        $criteria=new CDbCriteria;
        $criteria->select='t.id,t.content,t.content_over,t.create,t.reid,t.recount,t.from_type,t.from_iid,t.uid'; // only select the 'title' column
        $criteria->limit = $total;
        $criteria->offset =$first;
        $criteria->order='t.id DESC';
        $info = Topic::model()->with('author','images')->findAll($criteria);
        return $info;
    }

    public function shareTopic($uid,$message,$type,$share_id){
        $data = array(
            'allContent'=>$message,
            'content'=>$message,
            'ip'=>Xtutuquan::getClientIP(),
            'create'=>time(),
            'uid'=>$uid,
            'from_type'=>$type,
            'from_iid'=>$share_id,
            'reid'=>0,
            'recount'=>0
        );
        $db =  new Topic();
        $db->setAttributes($data);
        if($db->save()){
            return true;
        }else{
            return $db->getErrors();
        }
    }
    public function publish($allContent,$uid,$reid = 0){
        //微博内容分离
        $len = mb_strlen($allContent,'utf-8');
        $content = $content_over = '';
        if($len > 255){
            $content = mb_substr($allContent,0,255,'utf-8');
            $content_over = mb_substr($allContent,255,25,'utf-8');
        } else{
            $content = $allContent;
        }

        $db =  new Topic();
        $data = array(
            'allContent' =>$allContent,
            'content'=>$content,
            'ip'=>Xtutuquan::getClientIP(),
            'uid'=>$uid,
        );
        if(!empty($content_over)){
            $data['content_over'] = $content_over;
        }
        if($reid > 0){
            $data['reid'] = $reid;
        }


       $db->setAttributes($data);

       if($db->save()){
           $id = $db->attributes['id'];
           if($id){
               if($reid > 0) $this->reWbCount($reid);
               return $id;
           }else{
               return 0;
           }
       }else{
           return $db->getErrors();
       }

    }
    //原微博加一
    private function reWbCount($reid){
       $data = $this->findByPk($reid);
       $data->recount = $data->recount+1;
       $data->update();
    }
    /**
     * 显示时间
     */
     public function showTime($t){
        $time = time() - $t;
        if($time < 60){
            $str = '刚刚发布';
        }else if($time < 60 * 60){
            $str = floor($time / 60).'分钟前发布';
        }else if($time < 60 * 60 * 24){
            if(date('Y-m-d')==date('Y-m-d',$t)){
                $str = '今天'.date('H:i',$t);
            }else{
                $str = '昨天'.date('H:i',$t);
            }
        }else if($time < 60 * 60 * 48){
            if(date('Y-m-d',strtotime("-1 day"))==date('Y-m-d',$t)){
                $str = '昨天'.date('H:i',$t);
            }else{
                $str = date('m月d日',$t);
            }
        }else if($time < 60 * 60 * 24* 365){
            $str = date('m月d日',$t);
        }else{
            $str = date('Y年m月d日',$t);
        }
        return $str;
    }

    public function getRecontent($reid){
        $criteria=new CDbCriteria;
        $criteria->select='t.id,t.content,t.content_over,t.create,t.reid,t.recount,t.from_type,t.from_iid,t.uid'; // only select the 'title' column
        $criteria->addCondition("t.id=:reid");
        $criteria->params[':reid'] = $reid;
        $info = Topic::model()->with('author','images')->find($criteria);
        return $info;
    }


    public function showContent($content,$content_over,$staticsPath){
        $contents = $content.$content_over;
        $contents = preg_replace('/\[(a|b|c|d)_([0-9]+)\]/i','<img src="'.$staticsPath.'/face/$1/$2.gif" border="0">',$contents);

        //解析@账号
        $contents .= ' ';
        $pattern = '/(@\S+)\s/i';
        $contents = preg_replace($pattern,'<a class="space" href="'.Yii::app()->params['webPath'].'$1" target="_blank">$1</a>',$contents);
        return $contents;
    }

    public function showTextarea($content,$content_over,$staticsPath){
        $contents = $content.$content_over;
        $contents = preg_replace('/\[(a|b|c|d)_([0-9]+)\]/i','<img src="'.$staticsPath.'/face/$1/$2.gif" border="0">',$contents);
        return $contents;
    }


}